package mess;

import java.util.Arrays;

public class LeeCode_48 {
    public static void main(String[] args) {
        int[][] matrix = new int[][]{{1,2,3},{4,5,6},{7,8,9}};
        rotate(matrix);
        printMatrix(matrix);
    }
    private static void rotate(int[][] matrix) {
        int n = matrix.length;
        //水平翻转
        for (int i = 0; i < n / 2; i++) {
            int on = i;
            int under = n - i - 1;
            int[] temp = matrix[on];
            matrix[on] = matrix[under];
            matrix[under] = temp;
        }
        //对折
        for (int i = 0; i < n - 1; i++) {
            for (int j = i + 1; j < n; j++) {
                int temp = matrix[i][j];
                matrix[i][j] = matrix[j][i];
                matrix[j][i] = temp;
            }
        }
    }











    private static void printMatrix(int[][] matrix){
        int len = matrix.length;
        for (int i = 0; i < len; i++) {
            for (int j = 0; j < len; j++)
                System.out.print(matrix[i][j] + " ");
            System.out.println();
        }
    }
}
